package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import controller.Controladora;

@SuppressWarnings("serial")
public class JBMenu extends JMenuBar {
	
	private JMenu jmbRanz;
	private JMenuItem loadDB;
	private JMenuItem uploadDB;
	private JMenuItem loadCircuit;
	private JMenuItem logoff;
	private Controladora controladora;
	
	public JBMenu(ViewSession jf,FinestraMenu menu,Controladora controller) {
		super();
		this.controladora = controller;
		this.jmbRanz = new JMenu("Archive");
		this.loadDB = new JMenuItem("Load Session");
		this.uploadDB = new JMenuItem("Upload Session");
		this.loadCircuit = new JMenuItem("Load Circuit");
		this.logoff = new JMenuItem("Logoff");
		
		loadCircuit.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.Event.CTRL_MASK));
		loadDB.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.Event.CTRL_MASK));
		uploadDB.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_U, java.awt.Event.CTRL_MASK));
		logoff.setAccelerator(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.Event.CTRL_MASK));
		
		final URL urlLogOff = this.getClass().getResource("/view/imagenes/icons/power.png");
		java.awt.Image logOff = getToolkit().createImage(urlLogOff);
		this.logoff.setIcon(new ImageIcon(logOff));
		
		final URL urlLoadCircuit = this.getClass().getResource("/view/imagenes/icons/loadcircuit.png");
		java.awt.Image loadCrcuitIcon = getToolkit().createImage(urlLoadCircuit);
		this.loadCircuit.setIcon(new ImageIcon(loadCrcuitIcon));
		
		final URL urlLoadDB = this.getClass().getResource("/view/imagenes/icons/download.png");
		java.awt.Image loadDB = getToolkit().createImage(urlLoadDB);
		this.loadDB.setIcon(new ImageIcon(loadDB));
		
		final URL urlUploadDB = this.getClass().getResource("/view/imagenes/icons/upload.png");
		java.awt.Image UploadDB = getToolkit().createImage(urlUploadDB);
		this.uploadDB.setIcon(new ImageIcon(UploadDB));
		
		this.uploadDB.setEnabled(false);
		
		this.add(jmbRanz);
		this.jmbRanz.add(this.loadCircuit);
		this.jmbRanz.add(this.loadDB);
		this.jmbRanz.add(this.uploadDB);
		this.jmbRanz.add(this.logoff);
		
		crearListenerJMenuBar(jf,menu);
	}

	/**
	 * Crear els listeners dels menu items.
	 * @param jf
	 * @param menu
	 */
	public void crearListenerJMenuBar(final ViewSession jf, final JFrame menu){
		
		loadCircuit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String file = JOptionPane.showInputDialog("Enter new Circuit:");
				
				if(!jf.searchFile(file) && file != null ){
					JOptionPane.showMessageDialog(null, "Circuit not found!", "ERROR", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		loadDB.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controladora.carregaDades();
			}
		});
		
		uploadDB.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String sessionName = JOptionPane.showInputDialog("Session name:");
				if (controladora.pujaDades(sessionName)) {
					JOptionPane.showMessageDialog(null, "Session saved !", "OK", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		
		logoff.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				jf.resetApp();
				jf.setVisible(false);
				menu.setVisible(true);
			}
		});
		
	}

	/**
	 * S'activa la opció de carregar a la bbdd
	 */
	public void enableUploadDB() {
		uploadDB.setEnabled(true);
	}
	
	
	
}

